package com.neo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.event.HeartbeatMonitor;
import org.springframework.cloud.netflix.zuul.RoutesRefreshedEvent;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * <Description> 路由刷新事件发布，与事件监听者
 */
@Service
//public class RefreshRouteService implements ApplicationListener<ApplicationEvent> {
public class RefreshRouteService{

    @Autowired
    private ZuulHandlerMapping zuulHandlerMapping;

    private HeartbeatMonitor heartbeatMonitor = new HeartbeatMonitor();

    @Autowired
    ApplicationEventPublisher publisher;

    @Autowired
    private DiscoveryRouteLocator dynamicRouteLocator;

    /**
     * 动态路由实现 调用refreshRoute() 发布刷新路由事件
     */
    public void refreshRoute() {
        RoutesRefreshedEvent routesRefreshedEvent = new RoutesRefreshedEvent(dynamicRouteLocator);
        publisher.publishEvent(routesRefreshedEvent);
    }

    /**
     * 事件监听者。监控检测事件刷新
     *
     * @param event
     */
//    @Override
//    public void onApplicationEvent(ApplicationEvent event) {
//        if (event instanceof ContextRefreshedEvent || event instanceof RefreshScopeRefreshedEvent || event instanceof RoutesRefreshedEvent) {
//            //主动手动刷新。上下文刷新，配置属性刷新
//            zuulHandlerMapping.setDirty(true);
//        } else if (event instanceof HeartbeatEvent) {
//            //心跳触发，将本地映射关系。关联到远程服务上
//            HeartbeatEvent heartbeatEvent = (HeartbeatEvent) event;
//            if (heartbeatMonitor.update(heartbeatEvent.getValue())) {
//                zuulHandlerMapping.setDirty(true);
//            }
//        }
//    }
}